package com.rc.saas.mini.service.elec;

import com.rc.saas.mini.mapper.elec.ElecStationCollectMapper;
import com.rc.saas.mini.model.elec.ElecStationCollect;
import com.rc.saas.mini.model.elec.ElecStationCollectExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author wangzhengwei
 * @create 2021/4/22
 */
@Service
public class ElecStationCollectServiceImpl implements ElecStationCollectService{

    @Autowired
    private ElecStationCollectMapper elecStationCollectMapper;

    @Override
    public ElecStationCollectMapper getMapper() {
        return elecStationCollectMapper;
    }

    @Override
    public boolean selectCollectStatus(Long memberId, Long id) {
        ElecStationCollectExample elecStationCollectExample = new ElecStationCollectExample();
        elecStationCollectExample.createCriteria().andElecStationIdEqualTo(id).andMemberIdEqualTo(memberId);
        List<ElecStationCollect> list = elecStationCollectMapper.selectByExample(elecStationCollectExample);
        return !CollectionUtils.isEmpty(list);
    }

    @Override
    public void saveCollectStatus(String tenantCode,Long memberId, Long elecStationId, String opType) {
        ElecStationCollectExample example = new ElecStationCollectExample();
        example.createCriteria().andMemberIdEqualTo(memberId).andElecStationIdEqualTo(elecStationId);
        long count = elecStationCollectMapper.countByExample(example);

        //0 取消收藏 1收藏站点
        if ("0".equals(opType) && count > 0) {
            elecStationCollectMapper.deleteByExample(example);
        }else if ("1".equals(opType) && count < 1) {
            ElecStationCollect elecStationCollect = new ElecStationCollect();
            elecStationCollect.setMemberId(memberId);
            elecStationCollect.setElecStationId(elecStationId);
            elecStationCollect.setTenantCode(tenantCode);
            elecStationCollect.setCreatedTime(new Date());
            elecStationCollect.setCreatedBy(memberId.toString());
            elecStationCollectMapper.insertSelective(elecStationCollect);
        }
    }
}
